<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        function ajax(url,fnSucc,fnFail){
            //1.创建XHR对象
            var oAjax = new XMLHttpRequest();
            //2.建立连接发送请求
            oAjax.open('GET',url,true);
            oAjax.send();
            oAjax.onload = function(){
                if(oAjax.readyState==4 && oAjax.status==200){
                    fnSucc(oAjax.responseText)
                }else {
                    fnFail(oAjax.status)
                }
            }
        }

        window.onload = function() {
            let oBtn = document.getElementById('btn1');
            let oBox = document.getElementById('box1');
            oBtn.onclick = function(){
                let p1 = new Promise(function(resolve,reject){
                    ajax('ajax.txt',function(str){
                        resolve(str);
                    },function(str){
                        reject(str);
                    })
                })

                p1.then(function(str){
                    oBox.innerHTML = str;
                },function(){
                    oBox.innerHTML = str;
                })
            }
        }
    </script>
</head>
<body>
    <input type="button" value="aaa" id="btn1">
    <div id="box1"></div>
</body>
</html>
